/*
 * ProviderCMD.java
 * 
 * This command parses out the information needed to create a new provider,
 * creates the object, and adds it to the collection of providers in TourApp
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */

import java.util.*;

public class ProviderCMD extends Cmd
{
   private String name, servName;
   private int capacity, oH, oM, cH, cM;
   private GregorianCalendar openTime, closeTime;
   private Service service;
   
   public ProviderCMD() 
   { 
      super(); 
   }
   
   public void exec()
   {
      if( token.length == 6 )
      {
         name = token[ 1 ];
         servName = token[ 5 ];
         
         // parse ints from the tokens
         try
         {
            capacity = Integer.parseInt( token[ 2 ] );
            // hours and minutes of open/close times
            if( token[ 3 ].length() == 4 && token[ 4 ].length() == 4 )
            {
               oH = Integer.parseInt( token[ 3 ].substring( 0, 2 ) );
               oM = Integer.parseInt( token[ 3 ].substring( 2, 4 ) );
               cH = Integer.parseInt( token[ 4 ].substring( 0, 2 ) );
               cM = Integer.parseInt( token[ 4 ].substring( 2, 4 ) );
            }
            else
            {
               System.err.println( "Illegal time format" );
               return;
            }
         }
         catch ( NumberFormatException e ) //catch exception if bad token
         {
            System.err.println( "Error parsing token" );
            return;
         }
         
         // create GregorianCalendar times
         if( oH >= 0 && oH <= 23 && cH >= 0 && cH <= 24 &&
            oM >= 0 && oM <= 59 && cM >= 0 && cM <= 59 )
         {
            openTime = new GregorianCalendar( 0, 0, 0, oH, oM );
            closeTime = new GregorianCalendar( 0, 0, 0, cH, cM );
         }
         else
         {
            System.err.println( "Illegal time format" );
            return;
         }
         
         // check services collection to assure service exists
         if( tourApp.findService( servName ) )
            service = new Service( servName );
         else
         {
            System.err.println( "Illegal service" );
            return;
         }
         
         //**************************************************************************
         
         Provider newProvider = new Provider( name, capacity, openTime,
                                             closeTime, service );
         tourApp.addProvider( newProvider );
      }
      else
      {
         System.err.println( "Illegal number of token in command." );
         return;
      }
   }
}